package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.RoomInfo;
import com.atguigu.lease.web.app.mapper.RoomInfoMapper;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author xiongkunYang
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2025-04-19 22:27:34
 */
@Service
@RequiredArgsConstructor
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    private final RoomInfoMapper roomInfoMapper;
    private final BrowsingHistoryService browsingHistoryService;
    private final RedisTemplate<String, Object> redisTemplate;

    @Override
    public IPage<RoomItemVo> itemVoPageByQuery(long current, long size, RoomQueryVo queryVo) {
        Page<RoomItemVo> page = new Page<>(current, size);
        return this.roomInfoMapper.selectRoomItemVoPageByQuery(page, queryVo);
    }

    @Override
    public RoomDetailVo getDetailVoById(Long id) {
        String key = RedisConstant.APP_ROOM_PREFIX + id;
        RoomDetailVo roomDetailVo = (RoomDetailVo) this.redisTemplate.opsForValue().get(key);
        if (roomDetailVo == null) {
            roomDetailVo = this.roomInfoMapper.selectDetailVoById(id);
            this.redisTemplate.opsForValue().set(key, roomDetailVo);
        }
        this.browsingHistoryService.saveHistory(LoginUserContext.getLoginUser().getUserId(), id);
        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> itemVoPageByApartmentId(long current, long size, Long apartmentId) {
        Page<RoomItemVo> page = new Page<>(current, size);
        return this.roomInfoMapper.selectRoomItemVoPageByApartmentId(page, apartmentId);
    }
}




